/**
 *
 */
package anysware.file;

import java.io.File;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang.Validate;

import anysware.source.Source;

/**
 * TODO: doc me
 *
 * @author keke<keke@codehaus.org>
 * @revision $Revision: 111 $
 * @version
 */
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "source", "path" }))
public class FileItem implements Serializable {
	/**
	 *
	 */
	private static final long	serialVersionUID	= -1708548756260754410L;
	private Long	          id;
	private Date	          lastAccessed;
	@Column(nullable = false)
	private Date	          lastModified;
	private String	          name;
	private URL	              path;
	@OneToOne(optional = false)
	private Source	          source;
	@Transient
	private URI	              uri;

	public FileItem() {
		super();
	}

	public FileItem(final File file, final Source source) {
		Validate.notNull(file);
		Validate.notNull(source);
		this.source = source;
		lastModified = new Date(file.lastModified());
		name = file.getName();
		try {
			path = file.toURI().toURL();
		} catch (final MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final FileItem other = (FileItem) obj;
		if (path == null) {
			if (other.path != null) {
				return false;
			}
		} else if (!path.equals(other.path)) {
			return false;
		}
		if (source == null) {
			if (other.source != null) {
				return false;
			}
		} else if (!source.equals(other.source)) {
			return false;
		}
		return true;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
     * @return the lastAccessed
     */
    public Date getLastAccessed() {
    	return lastAccessed;
    }

	/**
	 * @return the lastModified
	 */
	public Date getLastModified() {
		return lastModified;
	}

	/**
	 * @return the title
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the path
	 */
	public URL getPath() {
		return path;
	}

	/**
	 * @return the source
	 */
	public Source getSource() {
		return source;
	}

	/**
	 * @return the uri
	 */
	@Transient
	public URI getUri() {
		return uri;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (path == null ? 0 : path.hashCode());
		result = prime * result + (source == null ? 0 : source.hashCode());
		return result;
	}

	/**
     * @param lastAccessed the lastAccessed to set
     */
    public void setLastAccessed(final Date lastAccessed) {
    	this.lastAccessed = lastAccessed;
    }

	/**
	 * @param lastModified
	 *            the lastModified to set
	 */
	public void setLastModified(final Date lastModified) {
		this.lastModified = lastModified;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setName(final String title) {
		name = title;
	}

	/**
	 * @param path
	 *            the path to set
	 */
	public void setPath(final URL path) {
		this.path = path;
	}

	/**
	 * @param source
	 *            the source to set
	 */
	public void setSource(final Source source) {
		this.source = source;
	}

	/**
	 * @param uri
	 *            the uri to set
	 */
	public void setUri(final URI uri) {
		this.uri = uri;
	}
}
